package com.wdd.myplatform.controller;

import com.wdd.myplatform.utils.MinioUtil;
import io.minio.*;
import io.minio.errors.MinioException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * minio使用demo
 */
@RestController()
@RequestMapping("/minioDemoController")
public class MinioDemoController {

    @Autowired
    private MinioUtil minioUtil;

    public static void main(String [] args){
        try {
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://192.168.127.129:9000")
                            .credentials("minioadmin", "minioadmin")
                            .build();

            // Make 'asiatrip' bucket if not exist.
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("firstbucket").build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("firstbucket").build());
            } else {
                System.out.println("Bucket 'firstbucket' already exists.");
            }
            // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
            // 'asiatrip'.
            ObjectWriteResponse writeResponse = minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket("firstbucket")
                            .object("creatTable.sql")
                            .filename("F:\\creatTable.sql")
                            .build());
            System.out.println(
                    "'F://测试.png' is successfully uploaded as "
                            + "object '测试.png' to bucket 'firstbucket'.");
        } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
            System.out.println("Error occurred: " + e);
//            System.out.println("HTTP trace: " + e.httpTrace());
        }
    }

    @GetMapping("/test")
    public void test(HttpServletResponse response){
//          minioUtil.removeObject("distribute-bucket1","test_1639378329623.xlsx");
//        minioUtil.removeBucket("distribute-bucket1");
//        minioUtil.createBucket("distribute-bucket1");
        minioUtil.createPublicBucket("distribute-bucket1");
//        String bucketPolicy = minioUtil.queryBucketPolicy("distribute-bucket1");
//        System.out.println(bucketPolicy);
//          minioUtil.download(response,"http://192.168.127.129:9000/distribute-bucket1/test_1639378329623.xlsx",null);
    }

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file){
        String upload = minioUtil.upload(file,"distribute-bucket1");
//        String upload = minioUtil.upload("D:\\creatTable.sql");
        System.out.println(upload);
        return upload;
    }

}
